<?php

class InventariosController extends AppController {
    
    public $sl_inventarios = array(
        "actions" => array("D" => "delete"),
        "filters" => array(
            array("name" => 'Inventario.id', "label" => 'Número'),
            array("name" => 'Stock.producto', "label" => 'Stock'),
            array("name" => 'Inventario.empresa_id', "label" => 'Sector', 'presentation' => 'SELECT', 'classparams' => '{"model":"Empresa"}'),
            array("name" => 'Inventario.delegacion_id', "label" => 'Delegación', 'presentation' => 'SELECT', 'classparams' => '{"model":"Delegacion"}')
        ),
        "columns" => array(
            array("name" => 'Inventario.id', "label" => 'Número'),
            array("name" => 'Stock.producto', "label" => 'Stock'),
            array("name" => 'Empresa.nombre', "label" => 'Empresa'),
            array("name" => 'Delegacion.nombre', "label" => 'Delegación')
        )
    );
    public $maint_inventarios = array(
        "fields" => array(
            "Datos del Inventario" => array(
                array("name" => "empresa_id", "label" => "Sector", 'presentation' => 'SELECT', 'classparams' => '{"model":"Empresa"}'),
                array("name" => "delegacion_id", "label" => "Delegación", 'presentation' => 'SELECT', 'classparams' => '{"model":"Delegacioon"}'),
                array("name" => "tipo_id", "label" => "Tipo Producto", 'presentation' => 'SELECT', 'classparams' => '{"model":"Tipo"}'),
                array("name" => "stock_id", "label" => "Stock Disponible", 'presentation' => 'STOCKS::DISPONIBLES', 'classparams' => '{"listen":"tipo_id"}')
            )
        )
    );

    public function isAuthorized() {
        if ($this->action === 'ajax_info') {
            return true;
        }
        return parent::isAuthorized();
    }
    
    public function add() {
        $this->maint = $this->maint_inventarios;
        parent::add();
    }

    public function edit($id = null) {
        $this->maint = $this->maint_inventarios;
        parent::edit($id);
    }

    public function index() {
        $this->search_list = $this->sl_inventarios;
        parent::index();
    }

    public function view($id = null) {
        $this->maint = $this->maint_inventarios;
        parent::view($id);
    }

    public function ajax_info($id = null) {
        if (!is_numeric($id)) {
            $array = array('status' => 'error', 'response' => 'INGRESE UN VALOR NUMERICO');
        } else {
            $this->Inventario->id = $id;
            if (!$this->Inventario->exists()) {
                $array = array('status' => 'error', 'response' => 'INVENTARIO INEXISTENTE');
            } else {
                $inventario = $this->Inventario->read(null, $id);
                $tipo = $this->Inventario->Stock->Tipo->find('list', array('conditions' => array('Tipo.id' => $inventario['Stock']['tipo_id'])));
                $problemas = $this->Inventario->Stock->Tipo->Problema->find('list', array('conditions' => array('Problema.tipo_id' => $inventario['Stock']['tipo_id'])));
                $array = array('status' => 'ok',
                               'problemas' => json_encode($problemas),
                               'tipo' => $tipo[$inventario['Stock']['tipo_id']],
                               'descripcion' => $inventario['Stock']['producto']);
            }
        }
        
        $this->set('data', json_encode($array));
        
        $this->render('/ajax', 'ajax');
    }

}